/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isPhoneNumber(phoneNum) {
  // var MobileRegex = /^1(3[0-9]|4[5,7]|5[0,1,2,3,4,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/
  let reg = /^1[3456789]\d{9}$/
  return reg.test(phoneNum);
}


export function isEmail(email) {
  let reg = /^\w+@[a-z0-9]+\.[a-z]{2,4}$/;
  return reg.test(email);
}
// console.log(isEmail("806024160@qq.com"));
/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}


// 手机和邮箱验证
export function isValidUsername(rule, value, callback) {
  var MobileRegex = /^1[3456789]\d{9}$/
  var mailRegex = /^\w*@[-a-zA-Z0-9]{1,20}\.[0-9a-z]{2,3}$/
  if (value === 'admin') {
    return callback()
  } else if (!MobileRegex.test(value)) {
    if (!mailRegex.test(value)) {
      return callback(new Error('请输入正确的手机或者邮箱!'))
    } else {
      return callback()
    }
  } else {
    return callback()
  }
}


// 手机验证
export function isValidPhone(rule, value, callback) {
  var MobileRegex = /^1[3456789]\d{9}$/
  if (!MobileRegex.test(value)) {
    return callback(new Error('请输入正确格式的手机号!'))
  } else {
    return callback()
  }
}
// 邮箱验证
export function isValidEmail(rule, value, callback) {
  var mailRegex = /^\w*@[-a-zA-Z0-9]{1,20}\.[0-9a-z]{2,3}$/
  if (!mailRegex.test(value)) {
    return callback(new Error('请输入正确的邮箱!'))
  } else {
    return callback()
  }
}

export function isValidCode(rule, value, callback) {
  if (value === '') {
    callback(new Error('请输入验证码'))
  } else if (value.length < 4) {
    callback(new Error('验证码格式错误'))
  } else {
    callback()
  }
}


// 输入框数字校验，这能输入正数和负数。小数
export function isValidNumber(rule, value, callback) {
  let reg = /^(\+\d+|\d+|\-\d+|\d+\.\d+|\+\d+\.\d+|\-\d+\.\d+)$/
  if (value === '') {
    callback(new Error('请输入数值'))
  } else if (reg.test(value)) {
    callback(new Error('请输入正确数值'))
  } else {
    callback()
  }
}
